import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // excludeComponents: [] as string[],
    excludeComponents: <string[]>[],
    isComposed: false,
    themeObj: {
      defaultTheme: {
        title: '浅色主题', // 主题名称
        mainColor: '#F4F5F5', // 全局主题色
        mainTextColor: '#303133', // 全局默认文字颜色
        mainBgColor: '#E9EEF3'// 全局默认背景色
      },
      darkTheme: {
        title: '深色主题',
        mainColor: '#01305F',
        mainTextColor: '#ffffff',
        mainBgColor: '#012447'
      }
    },
    currentTheme: 'defaultTheme',
    isShowLoading: false,
    showLoadingCount: 0, // 用于统计当前请求数，来判断是否loading
    userToken: JSON.parse(window.localStorage.getItem('userToken') || 'null')
  },
  mutations: {
    addExcludeComponent (state, excludeComponent) {
      const excludeComponents = state.excludeComponents
      if (Array.isArray(excludeComponent)) {
        state.excludeComponents = [...new Set([...excludeComponents, ...excludeComponent])]
      } else {
        state.excludeComponents = [...new Set([...excludeComponents, excludeComponent])]
      }
    },
    removeExcludeComponent (state, excludeComponent) {
      const excludeComponents = state.excludeComponents
      if (Array.isArray(excludeComponent)) {
        for (let i = 0; excludeComponent.length; i++) {
          const index = excludeComponents.findIndex(item => item === excludeComponent[i])
          if (index > -1) { excludeComponents.splice(index, 1) }
        }
      } else {
        for (let i = 0; i < excludeComponents.length; i++) {
          if (excludeComponents[i] === excludeComponent) {
            state.excludeComponents.splice(i, 1)
            break
          }
        }
      }
    },
    setListComposed (state, payload) {
      state.isComposed = payload
    },
    addAxiosShowLoading (state) {
      state.isShowLoading = true
      state.showLoadingCount++
    },
    removeAxiosShowLoading (state) {
      state.showLoadingCount--
      if (state.showLoadingCount === 0) {
        state.isShowLoading = false
      }
    },
    errorAxiosShowLoading (state) {
      state.isShowLoading = false
      state.showLoadingCount = 0
    },
    setUserToken (state, payload) {
      state.userToken = JSON.parse(payload)
      window.localStorage.setItem('userToken', payload)
    },
    setThemeName (state, payload) {
      state.currentTheme = payload
    }
  },
  actions: {
  },
  modules: {
  }
})
